package com.education.teach.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dn.res.wideget.CustomLoadMoreView;
import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.di.component.DaggerExamComponent;
import com.education.teach.di.module.ExamModule;
import com.education.teach.mvp.contract.ExamContract;
import com.education.teach.mvp.model.bean.ExamBean;
import com.education.teach.mvp.model.bean.LoginBean;
import com.education.teach.mvp.presenter.ExamPresenter;
import com.education.teach.mvp.ui.adapter.ExamAdapter;
import com.education.teach.utils.DnCommonUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import dn.easy.common.commonwidget.AVLoadingDialog;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 考试与作业
 */
public class ExamActivity extends BaseActivity<ExamPresenter> implements ExamContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.lv_content)
    RecyclerView mRecyclerView;
    @BindView(R.id.swf_pager_list)
    SwipeRefreshLayout mSwipeRereshLayout;

    int maxRow = 10;
    int page = 1;
    private ExamAdapter mAdapter;

    private LoginBean mLoginBean;
    private List<ExamBean.ResultBean.QueryListBean> mList = new ArrayList<>();

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerExamComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .examModule(new ExamModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_exam; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("考试与作业");
        mToolbar.setToolbarLeftBackImageRes(R.mipmap.top_back);
        mToolbar.setOnLeftTitleClickListener(view -> finish());
        mLoginBean = DnCommonUtils.getUserModel(this);
        mSwipeRereshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ExamAdapter(R.layout.item_exam, mList);
        CustomLoadMoreView mLoadMoreView = new CustomLoadMoreView();
        mAdapter.setLoadMoreView(mLoadMoreView);
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mAdapter.isFirstOnly(true);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {

            Bundle bundle = new Bundle();
            bundle.putSerializable("bean", mAdapter.getData().get(position).getPaper());
            Intent intent = new Intent(ExamActivity.this, ExamDetailActivity.class);
            intent.putExtras(bundle);
            launchActivity(intent);
        });

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
        });

        mPresenter.findMyHomeWork(mLoginBean.getResult().getXsXlh(), page, maxRow, true);
    }

    @Override
    public void showLoading() {
        AVLoadingDialog.showDialogForLoading(this);
    }

    @Override
    public void hideLoading() {
        if (mSwipeRereshLayout != null) mSwipeRereshLayout.setRefreshing(false);
        AVLoadingDialog.cancelDialogForLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.findMyHomeWork(mLoginBean.getResult().getXsXlh(), page, maxRow, true);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.findMyHomeWork(mLoginBean.getResult().getXsXlh(), page, maxRow, false);
    }

    @Override
    public void returnRefreshExamList(List<ExamBean.ResultBean.QueryListBean> queryList) {
        if (mSwipeRereshLayout != null) mSwipeRereshLayout.setRefreshing(false);
        if (queryList != null)
            mAdapter.setNewData(queryList);
        if (queryList == null || queryList.size() < maxRow)
            mAdapter.loadMoreEnd(true);
    }

    @Override
    public void returnLoadMoreExamList(List<ExamBean.ResultBean.QueryListBean> queryList) {
        mAdapter.loadMoreComplete();
        if (queryList == null || queryList.size() < maxRow)
            mAdapter.loadMoreEnd(true);
        mAdapter.addData(queryList == null ? new ArrayList<>() : queryList);
    }
}
